//
//  BusinessCertificationStep2Form.swift
//  iModern
//
//  Created by Ralf on 8/3/15.
//  Copyright (c) 2015 com.starlooking. All rights reserved.
//

import UIKit
import XLForm

class BusinessCertificationStep2Form: FormViewController
{
    var contactProfile = NSMutableDictionary()
    var second:  Int!
    var timer          = NSTimer()
    var sendRow: XLFormRowDescriptor!

    override func viewDidLoad()
    {
        super.viewDidLoad()
        second = 60
        self.navigationItem.title = "开设匠铺"
        self.initializeForm()
        self.tableView.editing = true
        self.initBackButton()
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }

    override func viewWillAppear(animated: Bool)
    {
        super.viewWillAppear(animated);
        MobClick.beginLogPageView("BusinessCertificationView2")

    }

    override func viewWillDisappear(animated: Bool)
    {
        super.viewWillDisappear(animated);
        MobClick.endLogPageView("BusinessCertificationView2")
    }

    func initializeForm()
    {
        var form:    XLFormDescriptor
        var section: XLFormSectionDescriptor
        var row:     XLFormRowDescriptor

        form = XLFormDescriptor(title: "")

        section = XLFormSectionDescriptor.formSectionWithTitle("")
        form.addFormSection(section)

        row = XLFormRowDescriptor(tag: "cellphone", rowType: XLFormRowDescriptorTypeText, title: "手机号")
        row.cellConfigAtConfigure["textField.placeholder"] = "请填写手机号码"
        row.required = true
        section.addFormRow(row)

        row = XLFormRowDescriptor(tag: "identifyingCode", rowType: XLFormRowDescriptorTypeText, title: "输入验证码")
        row.cellConfigAtConfigure["textField.placeholder"] = "请输入验证码"
        row.required = true
        section.addFormRow(row)



        sendRow = XLFormRowDescriptor(tag: "send", rowType: XLFormRowDescriptorTypeButton, title: "发送验证码")
        sendRow.action.formSelector = "sendTouchButton:"
        sendRow.required = true
//        row.disabled = NSNumber(bool: true)
        section.addFormRow(sendRow)



        row = XLFormRowDescriptor(tag: "next", rowType: XLFormRowDescriptorTypeButton, title: "下一步")
        row.cellConfigAtConfigure["backgroundColor"] = Theme.theme_color
        row.cellConfig["textLabel.color"] = UIColor.whiteColor()
        row.action.formSelector = "nextTouchButton:"
        row.required = true
        section.addFormRow(row)
        self.form = form
    }

    func nextTouchButton(sender: XLFormRowDescriptor)
    {
        let cellphone = self.formValues()["cellphone"] as? String
        if cellphone == nil
        {
            self.alertWarningMessage("请填写手机号码")
            return
        }
        let identifyingCode = self.formValues()["identifyingCode"] as? String
        if identifyingCode == nil
        {
            self.alertWarningMessage("请输入验证码")
            return
        }

        let rs = appDelegate.contactManager.verifyIdentifyingCode(cellphone, identifyingCode: identifyingCode)

        if rs == nil
        {
            self.alertNetworkConnectionException()
        }

        if rs!.hasErrors()
        {
            if rs?.errorCode == 3020
            {
                alertUserStatusException()
                NSNotificationCenter.defaultCenter().postNotificationName("Logout", object: nil)
                self.view.endEditing(true)
                navigationController?.popToRootViewControllerAnimated(true)
            }
            alertErrorMessage(rs!.errorMessage)
            return
        }

        self.contactProfile["cellphone"] = self.formValues()["cellphone"]
        self.contactProfile["identifyingCode"] = self.formValues()["identifyingCode"]
        let BusinessVC = BusinessCertificationStep3Form()
        BusinessVC.contactProfile = self.contactProfile
        self.pushViewController(BusinessVC)
    }

    func sendTouchButton(sender: XLFormRowDescriptor)
    {

        let cellphone = self.formValues()["cellphone"] as? String
        if cellphone == nil
        {

            self.alertWarningMessage("请填写手机号码")
            return
        }
        else
        {
            timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "timerFunction", userInfo: nil, repeats: true)

            timer.fire()
        }


        _ = appDelegate.contactManager.requestIdentifyingCode(cellphone)
    }

    func timerFunction()

    {
        second = second! - 1

        if second > 0 && second <= 60
        {
            let text = "\(second)秒后重新获取"

            sendRow.value = String(second!)

            sendRow.sectionDescriptor.formDescriptor.formRowWithTag("send")?.title = text

            sendRow.disabled = NSNumber(bool: true)

            let indexPath = NSIndexPath(forRow: 2, inSection: 0)
            let array     = NSArray(objects: indexPath)
            self.tableView.reloadRowsAtIndexPaths(array as! [NSIndexPath], withRowAnimation: UITableViewRowAnimation.None)

        }

        if second == 1
        {
            sendRow.sectionDescriptor.formDescriptor.formRowWithTag("send")?.title = "发送验证码"
            self.tableView.reloadData()
            sendRow.disabled = NSNumber(bool: false)
            timer.invalidate()
            second = 60
        }

    }

}
